<script setup lang="ts">
import { getDepAllAPI } from '@/apis/consult';
import type { TopDep } from '@/types/consult';
import { computed, onMounted, ref } from 'vue'

const active = ref(0)

const depAllList = ref<TopDep[]>([])

const getDepAllList = async () => {
    const res = await getDepAllAPI()
    // console.log(res)
    depAllList.value = res.data
}

// 在计算属性里获取二级科室 根据active的值来判断
const subDepList = computed(() => depAllList.value[active.value]?.child)

onMounted(() => {
    getDepAllList()
})

import { useConsultStore } from '@/stores'
// 使用
const store = useConsultStore()

</script>

<template>
  <div class="consult-dep-page">
    <cp-nav-bar title="选择科室" />
    <div class="wrapper">
      <van-sidebar v-model="active">
        <van-sidebar-item :title="item.name" v-for="item in depAllList" :key="item.id" />
      </van-sidebar>
      <div class="sub-dep">
        <router-link to="/consult/illness" v-for="supDep in subDepList" :key="supDep.id" @click="store.setDepType(supDep.id)" >{{ supDep.name }}</router-link>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.van-sidebar {
  width: 114px;
  &-item {
    padding: 14px;
    color: var(--cp-tag);
    &--select {
      color: var(--cp-main);
      font-weight: normal;
      &::before {
        display: none;
      }
    }
  }
}
.consult-dep-page {
  padding-top: 46px;
}
.wrapper {
  height: calc(100vh - 46px);
  overflow: hidden;
  display: flex;
  .sub-dep {
    flex: 1;
    height: 100%;
    overflow-y: auto;
    > a {
      display: block;
      padding: 14px 30px;
      color: var(--cp-dark);
    }
  }
}

</style>
